//
//  ButtonExample
//  Russell Lowke, June 30th 2013
//
//  Copyright (c) 2013 Lowke Media
//  see http://www.lowkemedia.com for more information
//  see http://code.google.com/p/lowke-buttoncontroller/ for code repository
//
//  Permission is hereby granted, free of charge, to any person obtaining a 
//  copy of this software and associated documentation files (the "Software"), 
//  to deal in the Software without restriction, including without limitation 
//  the rights to use, copy, modify, merge, publish, distribute, sublicense, 
//  and/or sell copies of the Software, and to permit persons to whom the 
//  Software is furnished to do so, subject to the following conditions:
// 
//  The above copyright notice and this permission notice shall be included in 
//  all copies or substantial portions of the Software.
// 
//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
//  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER  
//  DEALINGS IN THE SOFTWARE. 
//
//

package
{
    import com.lowke.buttonController.ButtonController;
    
    import flash.display.MovieClip;
    import flash.display.Sprite;
    import flash.events.MouseEvent;
    import flash.media.Sound;
    
    [SWF (width="200", height="100", backgroundColor="#555555", frameRate="30")]
    public class ButtonExample extends Sprite
    {
        public static const RED_STATE:String = "red";
        
        private var _clickSound:Sound = new ClickSound();
        private var _sampleButtonClip:MovieClip;
        private var _sampleButton:ButtonController;
        
        public function ButtonExample()
        {
            _sampleButtonClip = new SampleButton();
            _sampleButtonClip.x = stage.stageWidth/2;
            _sampleButtonClip.y = stage.stageHeight/2;
            addChild(_sampleButtonClip);
            
            _sampleButton = new ButtonController(_sampleButtonClip);
            _sampleButton.text = "Ready";
            _sampleButton.clickSound = _clickSound;
            _sampleButton.addKey(' ');  // respond to space key
            _sampleButton.addWeakListener(MouseEvent.CLICK, buttonClicked);
        }
        
        private function buttonClicked(event:MouseEvent):void
        {
            trace("buttonClicked()");
            
            if (_sampleButton.state == RED_STATE)
            {
                // normal state
                _sampleButton.state = "";
            }
            else
            {
                // "red" state
                // This causes ButtonController to look for "red_up", "red_over", and "red_down",
                // labels on the clip rather than the usual "up", "over", and "down" labels.
                _sampleButton.state = RED_STATE;
            }
        }
    }
}